package com.sun.alen.service.Impl;

import com.sun.alen.entity.PageBean;
import com.sun.alen.entity.Profession;
import com.sun.alen.mapper.ProfessionMapper;
import com.sun.alen.service.ProfessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProfessionServiceImpl implements ProfessionService {
    @Autowired
    private ProfessionMapper professionMapper;

    @Override
    public PageBean list(Integer currentPage, Integer pageSize) {
        //1. 设置分页参数
        //计算参数start
        Integer start = (currentPage-1)*pageSize;
        List<Profession> professions = professionMapper.list(start,pageSize);
        //2.查询总数
        Long count = professionMapper.count();
        PageBean pageBean = new PageBean(count,professions);
        return pageBean;
    }

    @Override
    public boolean add(Profession profession) {
        return professionMapper.add(profession);
    }

    @Override
    public boolean update(Profession profession) {
        return professionMapper.update(profession);
    }

    @Override
    public boolean delete(String id) {
        return professionMapper.delete(id);
    }

    @Override
    public PageBean search(String input, String select, Integer currentPage, Integer pageSize) {
        Integer start= (currentPage-1)*pageSize;
        List<Profession> professionList=new ArrayList<>();
        long count=0;
        input="%"+input+"%";
        switch (select){
            case "1":
                professionList=professionMapper.searchByName(input,start,pageSize);
                count=professionMapper.countByName(input);
                break;
            case "2":
                professionList=professionMapper.searchByType(input,start,pageSize);
                count=professionMapper.countByType(input);
                break;
            default:
                professionList=professionMapper.list(start,pageSize);
                count=professionMapper.count();
                break;
        }
        PageBean pageBean = new PageBean(count,professionList);
        return pageBean;
    }

    @Override
    public List<Profession> all() {
        return professionMapper.all();
    }
}
